/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Description: Project space tree.
 * Version: 1.0.0.4, Aug 3, 2010
 * Author: Liang Ding
 */

$(function(){
    projectSpaceTree.getProjectSpaceTree(); // XXX: empty argument
    projectSpaceTree.init();
});

projectSpaceTree = {
    refresh: function() {
        $("#projectSpaceTree").jstree("destroy");
        this.init();
    },
    getSelectedProject: function() {
        var pathArray = $("#projectSpaceTree").jstree("get_path");
        var ret = "";

        if (2 <= pathArray.length) { // ["Projects", "artifact"]
            ret = pathArray[1];
        }

        return ret;
    },
    getProjectSpaceTree: function(projectSpace) {
        var requestJSONObject = {}; // XXX: empty request json object
        var projectSpaceTree = jsonRpc.projectService.getProjectSpaceTree(
            requestJSONObject);
        nide.localStorage.setJSONObject("projectSpaceTree", projectSpaceTree);
    },
    /**
     * Gets the class path of selected Java file under project
     * ${basedir}src/main/java directory.
     *
     * @return the class path of selected Java file, for example:
     * org.b3log.Nide
     */
    getSelectedJavaFile: function() {
        var pathArray = $("#projectSpaceTree").jstree("get_path");
        var ret = "";
        for (var i = 5; /* skip the project space("Projects"), artifactId, src, main, java */
            i < pathArray.length; i++) {
            ret += pathArray[i];
            if (0 >= pathArray[i].indexOf(".")) { // is a directory
                ret += ".";
            }
        }

        return ret;
    },
    getSelectedFilePath: function() {
        var pathArray = $("#projectSpaceTree").jstree("get_path");
        var ret = "";
        
        for (var i = 1; // skip the project space("Projects") 
            i < pathArray.length; i++) {
            ret += pathArray[i];
            if (0 >= ret.indexOf(".")) { // is a directory
                ret += "/";
            }
        }

        return ret;
    },
    init: function() {
        $("#projectSpaceTree").jstree({
            "plugins": ["themes", "json_data", "types", "ui", "crrm"],
            "json_data": {
                "data": nide.localStorage.getJSONObject("projectSpaceTree")
            },
            "ui": {
                "select_limit": 1,
                "selected_parent_close": "select_parent",
                "initially_select": ["projectSpace"]
            },
            "types": {
                "max_depth": -2,
                "max_children": -2,
                "valid_children": ["projectSpace"],
                "types": {
                    "default": {
                        "valid_children": "none",
                        "icon": {
                            "image": "styles/file.png"
                        }
                    },
                    "file": {
                        "valid_children": "none",
                        "icon": {
                            "image": "styles/file.png"
                        }
                    },
                    "projectSpace": {
                        "valid_children": ["project"],
                        "icon": {
                            "image": "styles/file.png"
                        }
                    },
                    "folder": {
                        "valid_children": ["default", "folder"],
                        "icon": {
                            "image": "styles/folder.png"
                        }
                    },
                    "project": {
                        "valid_children": ["default", "folder"],
                        "icon": {
                            "image" : "styles/project.png"
                        }
                    }
                }
            },
            "core": {
                "initially_open" : ["projectSpace"],
                "animation": 0
            }
        })
        .bind("hover_node.jstree", function(e, data){
            });
    }
};
